package com.guiji.quartz.domain;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.task.DataSaveUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: wangxiaowen
 * @create: 2021-11-15 11:45
 **/
public class XinhuaBusiness {

	private static final Logger log = LoggerFactory.getLogger(XinhuaBusiness.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		String doc;
		try {
			response = HttpRequest.get("http://www.news.cn/english/list/china-business.htm")
				.header("Accept", "*/*")
				.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
				.header("Accept-Encoding", "gzip, deflate, br")
				.header("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
				.setConnectionTimeout(15000)
				.execute();
			doc = response.body();
			Set<String> resUrl = getUrl(doc);
			getContent(resUrl);
		} catch (Exception e) {
			log.error("http://www.news.cn/english/list/china-business.htm 代理失败1" + e);
			return;
		}


	}

	private static void getContent(Set<String> listContent) throws InterruptedException {
		if (null == listContent) {
			log.info("url list is null ");
			return;
		}
		for (String url : listContent) {
			Thread.sleep(1000);
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc = null;
			JXDocument jxDocument = null;
			try {
//				url = "http://www.news.cn/english/2021-11/15/c_1310311433.htm";
				HttpResponse response = HttpRequest.get(url)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
					.setConnectionTimeout(15000).execute();
				doc = response.body();
				if (null == doc || doc.isBlank() || doc.isEmpty()) {
					log.info("请求详情页为空");
					continue;
				}
				jxDocument = new JXDocument(doc);
			} catch (HttpException e) {
				log.info("请求详情页失败" + e);
				continue;
			}
			String title;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}

			StringBuffer content = new StringBuffer();
			String contentStr = null;
			try {
				jxDocument.sel("//div[@class='content']//p/text()").forEach(s -> {
					content.append(s + "$$$");
				});

				contentStr = content.toString();
			} catch (Exception e) {
				continue;
			}

			List<Object> newImgs = new ArrayList<>();
			String img = null;
			try {
				int imgSize = jxDocument.sel("//div[@class='content']//img/@src").size();
				if (imgSize > 0) {
					String temp = null;
					for (int k = 0; k < imgSize; k++) {
						temp = jxDocument.sel("//div[@class='content']//img/@src").get(k).toString();

						img = temp.strip();
						if (null != img && !img.isBlank() && !img.isEmpty()) {
							String temp1 = url.substring(0, "http://www.news.cn/english/2021-11/15/".length());
							img = new StringBuffer(temp1).append(img).toString();
							newImgs.add(img);
						}

					}
				}

			} catch (Exception e) {
				log.error("可能没有图片");
				img = "";
			}

			Date date = new Date();

			if (contentStr.length() > 50000 || contentStr.toString().replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "http://www.news.cn/english/list/china-business.htm", "XinhuaBusiness-EnglishBusiness");
				log.info(Thread.currentThread().getName() + "-----------------XinhuaBusiness-EnglishBusiness----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static Set<String> getUrl(String content) {

		String nid = null;
		String nidPattern = "nid: (.*?),";
		Pattern p1 = Pattern.compile(nidPattern);
		Matcher m1 = p1.matcher(content);
		while (m1.find()) {
			nid = (String) m1.group(1);
		}
		if (null != nid && !nid.isEmpty() && !nid.isBlank()) {
			HttpResponse response = null;
			String doc;
			try {
				String url = new StringBuffer("http://da.wa.news.cn/nodeart/page?nid=").append(nid).append("&pgnum=1&cnt=30&attr=63&tp=1&orderby=1&callback=jQuery112409454179515776757_1636947819046&_=1636947819047").toString();

				response = HttpRequest.get(url)
					.header("Accept", "*/*")
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
					.setConnectionTimeout(15000)
					.execute();
				doc = response.body();
				Set<String> res = new HashSet<>();
				String pattern = "http://www.news.cn/english/[0-9]{4}-[0-9]{2}/[0-9]{2}/.*?htm";
				Pattern p = Pattern.compile(pattern);
				Matcher m = p.matcher(doc);
				while (m.find()) {
					res.add(m.group());
				}
				return res;

			} catch (Exception e) {
				log.error("http://www.news.cn/english/list/china-business.htm 代理失败1" + e);
				return null;
			}
		} else {
			log.error("nid is null");
			return null;
		}


	}

}

